*
* Purpose: Tidy capacities
*


import excel "${capacities_raw}", sheet("Sheet1") firstrow clear

rename szint szint_string
gen szint = 1 if szint_string == "Bachelor"
replace szint = 2 if szint_string == "Master"
replace szint = 3 if szint_string == "Undivided"
replace szint = 4 if szint_string == "Degree program"
replace szint = 5 if szint_string == "Tertiary vocational"
replace szint = 6 if szint_string == "Supplementary"
drop szint_string

rename munkarend munkarend_string
gen munkarend = 1 if munkarend_string == "Regular"
replace munkarend = 2 if munkarend_string == "Correspondence"
replace munkarend = 3 if munkarend_string == "Evening"
replace munkarend = 4 if munkarend_string == "Distance learning"
drop munkarend_string

rename fin_form fin_form_string
gen fin_form = 1 if fin_form_string == "State-funded"
replace fin_form = 2 if fin_form_string == "Self-funded"
drop fin_form_string

replace capacity_max = capacity_min if capacity_max == . & capacity_min != .

gen eljaras = 1

merge 1:1 eljaras karkod karnev szekhely szaknev szakkod kepztelep szint ///
	munkarend fin_form ponthatar karkod_regi ev using "${institutions_raw}", keep(3) nogenerate
	
* ------------------------------------------------------------------------------
* Tidy up field of study -------------------------------------------------------
* ------------------------------------------------------------------------------
replace field_study = 9 if field_study == . & karkod == "DEZK"
replace field_study = 9 if field_study == . & karkod == "MOME"
replace field_study = 9 if field_study == . & karkod == "PTEMK"
replace field_study = 9 if field_study == . & karkod == "SZTEZMK"

replace jelszam = jelszam + 6 if szakkod == 17951
replace felvett = felvett + 1 if szakkod == 17951
replace jelszam = jelszam + 1 if szakkod == 17944
replace felvett = felvett + 0 if szakkod == 17944
drop if szakkod == 17952 | szakkod == 17943


gen capacity = .

order karkod szaknev kepztelep szint munkarend fin_form felvett jelszam ponthatar fin_form capacity* missing_in_booklet
bys field_study karkod szaknev kepztelep szint munkarend: gen program_count = _N

replace capacity = capacity_max if capacity_max > felvett & program_count == 1
replace capacity = felvett if capacity_max <= felvett & program_count == 1

replace capacity = felvett if program_count == 2 & fin_form == 1

gen felvett_funded = felvett if program_count == 2 & fin_form == 1
bys field_study karkod szaknev kepztelep szint munkarend: egen felvett_funded_max = max(felvett_funded)
replace capacity = capacity_max - felvett_funded_max if  program_count == 2 & fin_form == 2 & capacity_max - felvett_funded_max >= felvett
replace capacity = felvett if program_count == 2 & fin_form == 2 & capacity_max - felvett_funded_max < felvett

replace capacity = felvett if felvett_funded_max == 0

* Contracts that are not in the booklet:
replace capacity = felvett if missing_in_booklet == 1
replace capacity = felvett if missing_capacity == 1

*bys field_study karkod szaknev kepztelep szint munkarend: egen capacity_total = sum(capacity)

gen capacity_official = capacity_max
label variable capacity_official "Official capacity"
rename missing_capacity no_official_capacity
label variable no_official_capacity "No official capacity"
rename missing_in_booklet no_capacity_in_booklet
label variable no_capacity_in_booklet "The contract does not show up in the booklet"

keep field_study karkod szaknev szakkod kepztelep szint munkarend fin_form capacity capacity_official no_official_capacity no_capacity_in_booklet capacity_min

gen eljaras = 1

lab var eljaras "Admission procedure (1= main round)"
lab var karkod "Faculty code"
lab var szaknev "Major"
lab var szakkod "Major (code)"
lab var kepztelep "Location of major"
lab var szint "Level (Bachelor, Master, etc.)"
lab var munkarend "Schedule"
lab var fin_form "Funding (state-funded/self-funded)"
lab var capacity "Capacity"
lab var capacity_min "Capacity (min)"

compress
save "${temp_data_capacites}", replace
